package com.wen.controller;

import com.wen.common.ResultDto;
import com.wen.common.ResultDtoFactory;
import com.wen.controller.dto.CartItemDto;
import com.wen.controller.dto.OrderDto;
import com.wen.controller.dto.UserDto;
import com.wen.domain.User;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * springmvc各种参数
 */
@Api(value = "/mvc", description = "SpringMVC参数接口")
@Controller
@RequestMapping("mvc")
public class SpringMvcController {
    
    /**
     * 不同渲染方式
     *
     * @return
     */
    @RequestMapping("/index")
    public String index(ModelMap model) {
        model.addAttribute("model", "ModelMap 给页面渲染数据.");
        model.addAttribute("sexes", User.Sex.values());
        
        return "spring/index";
    }
    
    /**
     * 不同渲染方式
     *
     * @return
     */
    // @RequestMapping("/index")
    // public ModelAndView index() {
    // ModelAndView modelAndView=new ModelAndView("springMvc");
    // modelAndView.addObject("name","ModelAndView 给页面渲染数据.");
    //
    // return modelAndView;
    // }
    
    // ------------------------Get方式提交---------------------------------
    
    /**
     * a链接后面传递参数，跳转到原页面 通过路径方式获取不到,如：mvc/get/13735489562/石头.htm
     * 
     * @param phone
     * @param name
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ModelAndView get(@RequestParam String phone, @RequestParam String name) {
        ModelAndView modelAndView = new ModelAndView("spring/index");
        modelAndView.addObject("name", name);
        modelAndView.addObject("phone", phone);
        
        return modelAndView;
    }
    
    /**
     * a链接路径中传递参数，并跳转到原页面 url后面带参数形式，获取不到，如：/mvc/get.htm?name=wen&phone=13735159646
     * 
     * @param age
     * @param address
     * @return
     */
    @RequestMapping(value = "/get/{age}/{address}", method = RequestMethod.GET)
    public ModelAndView getPath(@PathVariable String age, @PathVariable String address) {
        ModelAndView modelAndView = new ModelAndView("spring/index");
        modelAndView.addObject("age", age);
        modelAndView.addObject("address", address);
        
        return modelAndView;
    }
    
    // -------------------------ajax提交--------------------------
    
    /**
     * get ajax方式传递参数,并返回json数据
     *
     * @param phone
     * @param name
     * @return
     */
    @ResponseHeader(name = "head1", description = "response head conf")
    // @ApiOperation(value = "查询详情", produces = "application/json", httpMethod = "GET", response = ResultDto.class)
    @ApiResponse(code = 0, message = "响应结果信息", reference = "ResultDto")
    @ApiResponses({@ApiResponse(code = 0, message = "响应结果信息", reference = "ResultDto")})
    @ResponseBody
    @RequestMapping(value = "/getJson", method = RequestMethod.GET)
    public ResultDto getJson(@ApiParam(value = "电话", required = true) String phone, String name) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phone", phone);
        map.put("name", name);
        
        ResultDto resultDto = ResultDtoFactory.success();
        resultDto.setData(map);
        
        return resultDto;
    }
    
    /**
     * get ajax方式，通过路径中传递参数
     *
     * @param sex
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPathJson/{sex}/{phone}", method = RequestMethod.GET)
    public ResultDto getPathJson(@PathVariable String sex, @PathVariable String phone) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("sex", sex);
        map.put("phone", phone);
        
        ResultDto resultDto = ResultDtoFactory.success(map);
        
        return resultDto;
    }
    
    /**
     * ajax提交json数组,传数组或者对象中有数组，必须加@RequestBody注解
     *
     * @param userDtoList
     * @return
     */
    @RequestMapping(value = "ajaxPostList", method = RequestMethod.POST)
    public ModelAndView ajaxPostList(@RequestBody List<UserDto> userDtoList) {
        ModelAndView view = new ModelAndView("/spring/ajaxDiv");
        view.addObject("data", userDtoList);
        
        return view;
    }
    
    /**
     * ajax post,参数对应接收
     *
     * @param productId
     * @param num
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ajaxPostParam", method = RequestMethod.POST)
    public ResultDto ajaxPostParam(Long productId, int num) {
        ResultDto resultDto = ResultDtoFactory.success();
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productId", productId);
        map.put("num", num);
        
        resultDto.setData(map);
        
        return resultDto;
    }
    
    /**
     * ajax post 提交，通过对象接收参数
     *
     * @param cartItemDto
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ajaxPostObject", method = RequestMethod.POST)
    public ResultDto ajaxPostObject(CartItemDto cartItemDto) {
        ResultDto resultDto = ResultDtoFactory.success();
        resultDto.setData(cartItemDto);
        
        return resultDto;
    }
    
    // ------------------------------表单提交-------------------------------
    
    /**
     * 表单页面
     * 
     * @param model 模型对象
     * @return
     */
    @RequestMapping(value = "form", method = RequestMethod.GET)
    public String fromIndex(ModelMap model) {
        model.addAttribute("sexes", User.Sex.values());
        
        return "spring/form";
    }
    
    /**
     * 表单提交
     *
     * @param userDto 用户数据对象
     * @return
     */
    @RequestMapping(value = "formSub", method = RequestMethod.POST)
    public ModelAndView formSubmit(UserDto userDto) {
        System.out.println("用户名" + userDto.getMobile());
        
        ModelAndView view = new ModelAndView();
        
        // 为空跳转新页面
        if ("" == userDto.getUserName()) {
            view.setViewName("redirect:redire.htm");
            view.addObject("nullObj", "为空,redirect");
            return view;
        }
        
        view.setViewName("spring/form");
        view.addObject("a", userDto);
        
        return view;
    }
    
    /**
     * Div内元素通过js，ajax提交
     * 
     * @return
     */
    @RequestMapping(value = "ajaxDiv", method = RequestMethod.GET)
    public String ajaxDiv() {
        return "spring/ajaxDiv";
    }
    
    /**
     * ajax post json字符串提交,也可以Map接收
     *
     * @param orderDto json数据对象
     */
    @ResponseBody
    @RequestMapping(value = "/ajaxPostJson", method = RequestMethod.POST)
    public ResultDto ajaxPostJson(@RequestBody OrderDto orderDto) {
        ResultDto resultDto = ResultDtoFactory.success();
        resultDto.setData(orderDto);
        
        return resultDto;
    }
    
    /**
     * redirect页面，并且取参数
     *
     * @param redirecParam
     * @return
     */
    @RequestMapping(value = "/redire", method = RequestMethod.GET)
    public ModelAndView formSubmit(@ModelAttribute("nullObj") String redirecParam) {
        ModelAndView view = new ModelAndView("/spring/redirect");
        view.addObject("param", redirecParam);
        
        return view;
    }
    
}
